<template>
  <a-form-model>
    <a-form-model-item label="再办人">
      <WorkDeptUserTree departId="3601001000" :type="'user'" :multiple="false" @save="treeSave" />
    </a-form-model-item>
    <a-form-model-item label="再办意见">
      <a-textarea v-model="Cre_work_repeat.remark" placeholder="请输入再办意见" style="height: 100px" />
    </a-form-model-item>
  </a-form-model>
</template>
<script>
import { saveCre_work_repeat } from '@/api/correction/cre_work_repeat.js'
import { nanoid } from 'nanoid'
import { viewCre_work } from '@/api/correction/cre_work.js'
import storage from 'store'
import { APP_USER } from '@/store/mutation-types.js'

export default {
  props: {
    workid: {
      type: String,
      require: true,
    },
    warnid: {
      type: String,
      require: true,
    },
  },
  data() {
    return {
      Cre_work: {},
      Cre_workNew: {
        userid: undefined, // 处置人id
        username: undefined, // 处置人名称
      }, // 新预警任务对象
      Cre_work_repeat: {
        remark: undefined, // 督办意见
        warnid: undefined, // 预警id
        workid: undefined, // 督办的任务id
      }, // 督办任务对象
      currentUser: storage.get(APP_USER), // 登录人信息
    }
  },
  created() {
    this.getWork(this.workid)
  },
  methods: {
    // 保存督办意见
    async save() {
      console.log('Cre_work_repeat', this.Cre_work_repeat)
      if (!this.Cre_workNew.id) {
        this.$message.warning('请选择再办人')
        return
      }
      if (!this.Cre_work_repeat.remark) {
        this.$message.warning('请填写再办意见')
        return
      }
      this.Cre_work_repeat.id = new Date().getTime() + nanoid()
      this.Cre_work_repeat.warnid = this.warnid
      this.Cre_work_repeat.workid = this.workid
      this.Cre_work_repeat.createuserid = this.currentUser.userId
      this.Cre_work_repeat.createusername = this.currentUser.fullname
      this.Cre_work_repeat.work = this.newWork()
      console.log('保存', this.Cre_work_repeat)
      if (!this.loading) {
        this.loading = true
        const { code } = await saveCre_work_repeat(this.Cre_work_repeat).finally(() => {
          this.loading = false
        })
        if (code == 0) {
          this.$message.success('操作成功')
          this.$emit('f_closeModal')
        }
      }
    },
    // 新增再办的处置任务
    newWork() {
      this.Cre_workNew.warnid = this.warnid
      this.Cre_workNew.workid = this.workid
      // this.Cre_workNew.worktype = this.Cre_work.worktype
      this.Cre_workNew.remark = this.Cre_work_repeat.remark
      this.Cre_workNew.repeatid = this.Cre_work_repeat.id
      return this.Cre_workNew
    },
    // 查询处置记录对象
    async getWork(workid) {
      const { data } = await viewCre_work(workid)
      if (null == data) {
        this.$message.error('未查询到处置记录')
        return
      }
      this.Cre_work = data
    },
    // 处置人树保存
    treeSave(obj) {
      console.log('处置人树保存', obj)
      if (obj.id) {
        this.Cre_workNew.id = new Date().getTime() + nanoid()
        this.Cre_workNew.userid = obj.id
        this.Cre_workNew.username = obj.name
      } else {
        this.$message.warning('未获取到人员人id')
      }
    },
  },
  components: {
    WorkDeptUserTree: () => import('@/views/correction/work/WorkDeptUserTree.vue'),
  },
}
</script>

<style lang="scss" scoped>
</style>
